// 检查数据库表结构
const { Pool } = require('pg')

const pool = new Pool({
  host: 'localhost',
  port: 5432,
  database: 'aide',
  user: 'postgres',
  password: 'unidt@123'
})

async function checkTableStructure() {
  try {
    console.log('🔍 检查数据库表结构...\n')
    
    // 检查 recurring_task_instances 表
    const instancesResult = await pool.query(`
      SELECT column_name, data_type, is_nullable 
      FROM information_schema.columns 
      WHERE table_name = 'recurring_task_instances' 
      ORDER BY ordinal_position
    `)
    
    console.log('📋 recurring_task_instances 表结构:')
    if (instancesResult.rows.length > 0) {
      instancesResult.rows.forEach(row => {
        console.log(`  ${row.column_name}: ${row.data_type} (${row.is_nullable === 'YES' ? 'nullable' : 'not null'})`)
      })
    } else {
      console.log('  ❌ 表不存在')
    }
    
    console.log('')
    
    // 检查 recurring_tasks 表
    const tasksResult = await pool.query(`
      SELECT column_name, data_type, is_nullable 
      FROM information_schema.columns 
      WHERE table_name = 'recurring_tasks' 
      ORDER BY ordinal_position
    `)
    
    console.log('📋 recurring_tasks 表结构:')
    if (tasksResult.rows.length > 0) {
      tasksResult.rows.forEach(row => {
        console.log(`  ${row.column_name}: ${row.data_type} (${row.is_nullable === 'YES' ? 'nullable' : 'not null'})`)
      })
    } else {
      console.log('  ❌ 表不存在')
    }
    
    console.log('')
    
    // 检查 tasks 表中重复任务相关字段
    const taskFieldsResult = await pool.query(`
      SELECT column_name, data_type, is_nullable 
      FROM information_schema.columns 
      WHERE table_name = 'tasks' 
      AND column_name IN ('is_recurring', 'recurring_type', 'recurring_pattern', 'parent_recurring_id')
      ORDER BY ordinal_position
    `)
    
    console.log('📋 tasks 表中重复任务字段:')
    if (taskFieldsResult.rows.length > 0) {
      taskFieldsResult.rows.forEach(row => {
        console.log(`  ${row.column_name}: ${row.data_type} (${row.is_nullable === 'YES' ? 'nullable' : 'not null'})`)
      })
    } else {
      console.log('  ❌ 重复任务字段不存在')
    }
    
  } catch (error) {
    console.error('❌ 检查表结构失败:', error.message)
  } finally {
    await pool.end()
  }
}

checkTableStructure()
